
package com.fast.fastenjoy.base.verify.rule;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;

public abstract class VerifyTest {

	private static final Map<VerifyRule, VerifyTest> COLUMN_TEST_MAP = new HashMap<VerifyRule, VerifyTest>();
	
	private static final Map<VerifyRule, String> COLUMN_TEST_MAP_MSG = new HashMap<VerifyRule, String>();
	
//	private static final String METADATA_MANAGER_BEAN_NAME = "system-MetadataManager";
	
	private static final String CUST_VERIFY_RULE_KEY_REGEX = "\\$\\{.*\\}";
	
	private static final String CUST_VERIFY_RULE_JSON__KEY_MSG = "msg";
	
	private static final String CUST_VERIFY_RULE_JSON__KEY_REGEX = "regex";
	
	public static final String REGEX_EMAIL = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
	public static final String REGEX_URL = "[a-zA-z]+://[^\\s]**";
	public static final String REGEX_IP = "\\d+\\.\\d+\\.\\d+\\.\\d+";
	public static final String REGEX_CN_CARD = "\\d{15}|\\d{18}";
	
	static{
		
		try{
			COLUMN_TEST_MAP_MSG.put(VerifyRule.INT, "必须是数字");
			COLUMN_TEST_MAP_MSG.put(VerifyRule.EMAIL, "必须是邮箱格式");
			COLUMN_TEST_MAP_MSG.put(VerifyRule.URL, "必须是URL格式");
			COLUMN_TEST_MAP_MSG.put(VerifyRule.IP, "必须是IP格式");
			COLUMN_TEST_MAP_MSG.put(VerifyRule.CN_CARD, "必须是身份证号码");
			
			new VerifyTest(VerifyRule.INT) {
				public boolean test(String value) {
					return StringUtils.isNumeric(value);
				}
			};
			
			new VerifyTest(VerifyRule.EMAIL) {
				public boolean test(String value) {
					return value != null && value.matches(REGEX_EMAIL);
				}
			};
			
			new VerifyTest(VerifyRule.URL) {
				public boolean test(String value) {
					return value != null && value.matches(REGEX_URL);
				}
			};
			
			new VerifyTest(VerifyRule.IP) {
				public boolean test(String value) {
					return value != null && value.matches(REGEX_IP);
				}
			};
			
			new VerifyTest(VerifyRule.CN_CARD) {
				public boolean test(String value) {
					return value != null && value.matches(REGEX_CN_CARD);
				}
			};
			
			new VerifyTest(VerifyRule.OTHER) {
				
				@Override
				public String fieldVerify(String title, String custVerifyRule, String value){
					try{
						
						if(StringUtils.isEmpty(custVerifyRule)){
							return StringUtils.EMPTY;
						}
						
						String _json = custVerifyRule;
						if(custVerifyRule.matches(CUST_VERIFY_RULE_KEY_REGEX)){
							String verifyRuleKey = custVerifyRule.substring(2, custVerifyRule.length() - 1);
							_json = getCustVerifyRule(verifyRuleKey);
						}
						
						if(StringUtils.isEmpty(_json)){
							return StringUtils.EMPTY;
						}
							
						JSONObject json = JSONObject.parseObject(_json);
						
						String regex = json.getString(CUST_VERIFY_RULE_JSON__KEY_REGEX);
						if(StringUtils.isNotEmpty(regex)){
							if(value != null){
								if(!value.matches(regex)){
									String msg = json.getString(CUST_VERIFY_RULE_JSON__KEY_MSG);
									if(StringUtils.isEmpty(msg)){
										return new StringBuffer(title).append("不正确").toString();
									}else{
										return new StringBuffer(title).append(msg).toString();
									}
								}
							}
						}
						
					}catch(Exception e){
						e.printStackTrace();
					}
					
					return StringUtils.EMPTY;
				}
				
				public boolean test(String value) {
					return true;
				}
			};
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static String getCustVerifyRule(String verifyRuleKey){
		
		if(StringUtils.isEmpty(verifyRuleKey)){
			return null;
		}
		
//		MetadataManager metadataManager = ApplicationContextUtils.getBean(METADATA_MANAGER_BEAN_NAME, MetadataManager.class);
//		
//		if(metadataManager != null){
//			MetadataDto metadata = metadataManager.getMetadata(verifyRuleKey);
//			if(metadata != null){
//				return metadata.getRmk();
//			}
//		}
		
		return null;
	}
	
	public static VerifyTest getColumnTest(VerifyRule verifyRule){
		return COLUMN_TEST_MAP.get(verifyRule);
	}
	
	public static VerifyTest getOtherColumnTest(){
		return COLUMN_TEST_MAP.get(VerifyRule.OTHER);
	}
	
	public VerifyTest(VerifyRule verifyRule){
		COLUMN_TEST_MAP.put(verifyRule, this);
	}
	
	public String fieldVerify(String title, String custVerifyRule, String value){
		if(test(value)){
			return StringUtils.EMPTY;
		}else{
		}
		return StringUtils.EMPTY;
	}
	
	public abstract boolean test(String value);
	
}
